package b1.scenes.fight
{
	import Box2D.Collision.*;
	import Box2D.Collision.Shapes.*;
	import Box2D.Common.Math.*;
	import Box2D.Dynamics.*;
	
	import b1.conifg.*;
	import b1.players.*;
	import b1.scenes.*;
	
	import com.greensock.*;
	
	public class Frog extends Animal
	{
		//舌头
		private var tongue:FrogTongue;
		//舌头角度
		private var tongueAngle:Number;
		//舌头基础长度
		private const tongueBaseLength:Number = 358;
		//舌头长度
		private var tongueLength:Number;
		
		public function Frog(animalId:int, leftFlag:Boolean)
		{
			super(animalId, leftFlag);
		}
		
		//发射准备的骨头
		public function launchPrepareBone(boneIndex:int):void
		{
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			//由若干个骨头集结而成
			var mass:Number = 0;
			var boneConfig:ObjectConfig = Config.objectConfigs[boneIndex];
			var existTime:Number = 10;
			var bonePosX:Number;
			var bonePosY:Number;
			if (leftFlag)
			{
				bonePosX = tongue.x + Math.cos((tongueAngle - 15) * Math.PI / 180) * tongueLength;
				bonePosY = tongue.y + Math.sin((tongueAngle - 15) * Math.PI / 180) * tongueLength;
			}
			else
			{
				bonePosX = tongue.x - Math.cos((tongueAngle + 15) * Math.PI / 180) * tongueLength;
				bonePosY = tongue.y - Math.sin((tongueAngle + 15) * Math.PI / 180) * tongueLength;
			}
			var bone:Bone; 
			if (boneConfig.splitBoneNum > 0)
			{
				var boneCluster:BoneCluster = new BoneCluster(bonePosX, bonePosY, boneConfig.splitBoneIndex, boneConfig.splitBoneNum);
				for (var i:int = 0; i < boneCluster.bones.length; i++)
				{
					bone = boneCluster.bones[i];
					mass += bone.objectB2Body.GetMass();
					fight.objectLayer.addChild(bone);
					TweenLite.delayedCall(existTime, bone.destroy); 
				} 
				TweenLite.delayedCall(0.2, boneCluster.split);
			}
			else
			{
				bone = new Bone(boneIndex);
				var boneb2Body:b2Body = bone.createB2Body(fight.world, bonePosX, bonePosY, bone.width, bone.height, boneConfig.density, boneConfig.restitution);
				boneb2Body.SetAngle(this.rotation * Math.PI / 180);
				TweenLite.delayedCall(existTime, bone.destroy); 
				mass += bone.objectB2Body.GetMass();
				fight.objectLayer.addChild(bone);
			}
			var torque:Number = b2Math.RandomRange(0, 200) * mass;
			bone.applyForce(0, 0, torque)
		}
		
		//召唤
		public override function summon():void
		{
			super.summon();
			progressBar.resume();
			TweenLite.delayedCall(5, startWalk);
		}
		
		//消失
		public override function unsummon():void
		{
			super.unsummon();
			TweenLite.delayedCall(6, destroy);
		}
		
		//进度条到达右侧时候的动作
		public override function onAction():void
		{
			var length:Number = b2Math.RandomRange(tongueBaseLength, tongueBaseLength + 300);
			strechOutTongue(length);
		}
		
		//伸出舌头
		public function strechOutTongue(length:Number):void
		{
			if (tongue != null)
			{
				tongue.destroy();
			}
			tongue = new FrogTongue(5, leftFlag);
			var fight:Fight = SceneContainer.getInstance().currentScene as Fight;
			var tonguePosX:Number = 30;
			tongueAngle = b2Math.RandomRange(-15, -45);
			if (leftFlag)
			{
				tongue.x = this.x + tonguePosX;
			}
			else
			{
				tongue.x = this.x - tonguePosX;
				tongueAngle = -tongueAngle;
			}
			tongueLength = length; 
			tongue.y = this.y - 42;
			tongue.scaleX = tongueLength / tongueBaseLength;
			tongue.createB2Body(fight.world, tongue.x, tongue.y, 100 * tongue.scaleX, 100, 1, 0.5, false);
			tongue.objectB2Body.SetAngle(Math.PI * tongueAngle / 180);
			tongue.strechOut();	
			fight.objectLayer.addChild(tongue);
			TweenLite.delayedCall(1, drawBackTongue);
		}
		
		//缩回舌头
		public function drawBackTongue():void
		{
			if (tongue != null)
			{
				tongue.destroyB2Body();
				tongue.drawBack();
				var animalInfo:AnimalInfo = PlayerInfoHelper.getInstance().animals[this.animalId];
				//扔骨头
				launchPrepareBone(animalInfo.userData1);
			}
		}
		
		public override function destroy():void
		{
			super.destroy();
			if (tongue)
				tongue.destroy();
			if (this.parent != null)
				this.parent.removeChild(this);
		}
	}
}